//
//  UIBarButtonItem+Category.m
//  PatientiOS
//
//  Created by MaShaoHua on 2024/4/28.
//

#import "UIBarButtonItem+Category.h"
#import <objc/runtime.h>
#import "UIButton+Awesome.h"

@interface UIBarButtonItem ()
@property (nonatomic) BarButtonItemBlock barButtonItemBlock;
@end

@implementation UIBarButtonItem (Category)

+(instancetype)barButtonItemWithImage:(NSString *)image andBarButtonItemBlock:(BarButtonItemBlock)barButtonItemBlock
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//                     buttonWithImageName:image];
    [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    btn.frame = CGRectMake(0, 0, 30, 44);
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 8);
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    item.barButtonItemBlock = barButtonItemBlock;
    
    [btn addTarget:item action:@selector(clickItem) forControlEvents:UIControlEventTouchUpInside];
    
    return item;
}

+(instancetype)barButtonRightItemWithImage:(NSString *)image andBarButtonItemBlock:(BarButtonItemBlock)barButtonItemBlock
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//                     buttonWithImageName:image];
    [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    btn.frame = CGRectMake(0, 0, 30, 44);
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    item.barButtonItemBlock = barButtonItemBlock;
    
    [btn addTarget:item action:@selector(clickItem) forControlEvents:UIControlEventTouchUpInside];
    return item;
}

+(instancetype)barButtonItemWithTitle:(NSString *)title andBarButtonItemBlock:(BarButtonItemBlock)barButtonItemBlock
{
    //默认是白色文字
    return [self barButtonItemWithTitle:title titleColor:[UIColor colorWithRed:51 green:51 blue:51 alpha:1] andBarButtonItemBlock:barButtonItemBlock];
}

+(instancetype)barButtonItemWithTitle:(NSString *)title titleColor:(UIColor *)titleColor andBarButtonItemBlock:(BarButtonItemBlock)barButtonItemBlock
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn sizeToFit];
    btn.frame = CGRectMake(0, 0, 40, 24);
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    item.barButtonItemBlock = barButtonItemBlock;
    [btn addTarget:item action:@selector(clickItem) forControlEvents:UIControlEventTouchUpInside];
//    [item setTarget:item];
//    [item setAction:@selector(clickItem)];
//    item.tintColor = titleColor;
//    [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:FONT(15),NSFontAttributeName, nil] forState:UIControlStateNormal];
    return item;
}
- (void)clickItem
{
    if (self.barButtonItemBlock) {
        self.barButtonItemBlock();
    }
}



static void *key = &key;
-(void)setBarButtonItemBlock:(BarButtonItemBlock)barButtonItemBlock
{
    objc_setAssociatedObject(self, & key, barButtonItemBlock, OBJC_ASSOCIATION_COPY);
}
-(BarButtonItemBlock)barButtonItemBlock
{
    return objc_getAssociatedObject(self, &key);
}
@end
